import { KeyboardEvent, SelectionChange } from './event'
import { TextModel, SelectionModel } from './models'
import { InsertTextOperation, SetSelectionOperation, RemoveTextOperation } from './operations'
import Operation from "./operations/operation"
// import { RenderView } from './view'
import { ViewProvider } from './view/provider/view_provider'

class Editor {
    private textModel_!: TextModel;
    private selectionModel_!: SelectionModel;
    // private view_!: RenderView;
    private view_!: any;
    private viewProvider_!: ViewProvider;
    private eventHandler!: KeyboardEvent;
    private container;
    private selectionChange!: SelectionChange

    constructor(container: HTMLElement) {
        this.container = container
        this.viewProvider_ = new ViewProvider()
    }
    init() {
        this.eventHandler = new KeyboardEvent(this, this.container)
        this.textModel_ = new TextModel()
        this.selectionModel_ = new SelectionModel(this.textModel_)
        this.view_ = this.viewProvider_.provide('source_and_preview', this.textModel_, this.selectionModel_, this.container)
        // this.view_ = new RenderView(this.textModel_, this.selectionModel_, this.container)
        this.selectionChange = new SelectionChange(this, this.container, this.selectionModel_)
        this.eventHandler.addListeners()
        this.selectionChange.addListeners()

    }

    getView() {
        return this.view_
    }

    getTextModel() {
        return this.textModel_
    }

    getSelectionModel() {
        return this.selectionModel_
    }

    apply(operation: Operation) {
        // if (operation instanceof InsertTextOperation) {
        //     let spacers: string = operation.getSpacers().toUpprCase();
        //     operation.setSpacers(spacers)
        // }
        operation.apply(this);
    }
    // 插入文本
    insertTextAtCursor(text: string) {
        const selection = this.selectionModel_.getSelection()
        let startIndex = selection?.anchor
        let cursorIndex;
        if (!this.selectionModel_.isCollapsed()) {
            const removeTextOperation = new RemoveTextOperation(selection.anchor, selection.focus)
            this.apply(removeTextOperation)
            if (this.selectionModel_.isBackward()) {
                startIndex = selection.focus
            }
        }
        cursorIndex = startIndex + text.length
        const insertTextOperation = new InsertTextOperation(text, startIndex)
        this.apply(insertTextOperation)
        this.setSelection(cursorIndex, cursorIndex)

    }
    // 删除文本
    deleteTextAtCursor() {
        const selection = this.selectionModel_.getSelection()
        let startIndex = selection?.anchor
        let endIndex = selection?.focus
        if (startIndex <= 0) return
        if (!this.selectionModel_.isCollapsed()) {
            const removeTextOperation = new RemoveTextOperation(startIndex, endIndex)
            this.apply(removeTextOperation)
            if (this.selectionModel_.isBackward()) {
                startIndex = selection.focus
            }
        } else if (selection.anchor > 0) {
            const removeTextOperation = new RemoveTextOperation(startIndex - 1, endIndex)
            this.apply(removeTextOperation)
            startIndex--
        }
        this.setSelection(startIndex, startIndex)
    }
    // Enter 事件
    insertEnter() {
        const selection = this.selectionModel_.getSelection()
        let startIndex = selection?.anchor

        let cursorIndex: number = 0
        if (!this.selectionModel_.isCollapsed()) {
            const removeTextOperation = new RemoveTextOperation(selection.anchor, selection.focus)
            this.apply(removeTextOperation)
            if (this.selectionModel_.isBackward()) {
                startIndex = selection.focus
            }
        }
        cursorIndex = startIndex + 1
        const insertTextOperation = new InsertTextOperation('\n', startIndex)
        this.apply(insertTextOperation)
        this.setSelection(cursorIndex, cursorIndex)
        // 如果是字符中间输入换行，那么需要删除前面故意增加的空格
        if (!this.selectionModel_.isEnd()) {
            this.setSelection(cursorIndex + 1, cursorIndex + 1)
            this.deleteTextAtCursor()
        }
    }
    // 撤销
    undo() {

    }
    // 回退
    redo() {

    }
    // 设置选区
    setSelection(anchor: number, focus: number) {
        const setSelectionOperation = new SetSelectionOperation({ anchor, focus })
        this.apply(setSelectionOperation)
    }
}
export default Editor;
